package com.cloudcentral.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloudcentral.entity.vo.PageVO;
import com.cloudcentral.exception.FailureException;
import com.cloudcentral.service.DetectResultBestService;
import com.cloudcentral.util.Result;
import com.cloudcentral.entity.DetectResultBest;
import com.cloudcentral.entity.query.DetectResultBestGenerateQuery;
import com.cloudcentral.entity.query.DetectResultBestQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 探测结果优选记录表 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2023-11-23
 */
@Slf4j
@RestController
@RequestMapping("/detect-result-best")
@Tag(name = "探测结果优选")
public class DetectResultBestController extends BaseController {
  private final DetectResultBestService detectResultBestService;

  @Autowired
  public DetectResultBestController(DetectResultBestService detectResultBestService) {
    this.detectResultBestService = detectResultBestService;
  }

  @PostMapping("/generate")
  @Operation(summary = "生成探测优选结果")
  public Result<Boolean> generate(DetectResultBestGenerateQuery query) throws Exception {
    if (query.getMasterId() == null && query.getSlaveId() == null) throw new FailureException("缺少台站ID");
    else if (query.getConditions().isEmpty()) throw new FailureException("缺少优选条件");
    else return Result.ok(detectResultBestService.generate(query));
  }

  @DeleteMapping("/delete")
  @Operation(summary = "删除")
  public Result<Boolean> delete(Long id) throws Exception {
    return Result.ok(detectResultBestService.delete(id));
  }

  @GetMapping("/listPage")
  @Operation(summary = "分页查询")
  public Result<PageVO<DetectResultBest>> listPage(DetectResultBestQuery query) {
    QueryWrapper<DetectResultBest> queryWrapper = new QueryWrapper<>();
    if (query.getMasterId() != null) queryWrapper.eq("master_id", query.getMasterId());
    if (query.getSlaveId() != null) queryWrapper.eq("slave_id", query.getSlaveId());
    if (query.getBeginDate() != null && query.getEndDate() != null) queryWrapper.between("generate_time", query.getBeginDate(), query.getEndDate());
    IPage<DetectResultBest> iPage = detectResultBestService.page(new Page<>(query.getPage(), query.getSize()), queryWrapper);
    return Result.ok(new PageVO<>(iPage.getRecords(), iPage.getTotal()));
  }
}
